/*
     Change implementations.
*/

AOSWP_Change = function ($aosl_change_doc, canvas) {
    var object_id = $aosl_change_doc.attr("object");
    var change_type = $aosl_change_doc.prop("nodeName");
    var was_successful = false;

    var CHANGE_IMPL = {
        "activate": {
            apply: function () {
                return canvas.activate_object(object_id);
            }
          , reverse: function () {
              return canvas.deactivate_object(object_id);
          }
        }
    , "deactivate": {
        apply: function () {
            return canvas.deactivate_object(object_id);
        }
        , reverse: function () {
            return canvas.activate_object(object_id);
        }
    }
    , "switch": {
        apply: function () {
            return canvas.switch_object(object_id);
        }
        , reverse: function () {
            return canvas.switch_object(object_id);
        }
    }
    , "transform":
        {
            apply: function () {

                $aosl_change_doc.children("absolute,relative").each(function () {
                    var transformation = AOSWP_Transformation(null,$(this));
                    var is_relative_to_parent = ($(this).prop("nodeName") == "relative");
                    canvas.transform_object(object_id, transformation, is_relative_to_parent);
                });

                return true;
            }
        , reverse: function () {
            $aosl_change_doc.children("absolute,relative").each(function () {
                var transformation = AOSWP_Transformation(null, $(this)).inverse();
                var is_relative_to_parent = ($(this).prop("nodeName") == "relative");
                canvas.transform_object(object_id, transformation, is_relative_to_parent);
            });

            return true;
        }
        }
    };

    return {
        apply: function () {
            was_successful = CHANGE_IMPL[change_type].apply();
        }
    , reverse: function () {
            if (was_successful)
                CHANGE_IMPL[change_type].reverse();
        }
    }
}